﻿namespace NTLib.Core.Threading
{
    using NTLib.Core.Memory;
    using System;
    using System.Runtime.CompilerServices;
    using System.Threading;
    using System.Threading.Tasks;

    /// <summary>
    /// Implementation of the ILocker interface with no thread lock.
    /// This type must be used to remove the thread safe implementation.
    /// </summary>
    public class NoLocker : Disposable, ILocker
    {
        #region Methods

        /// <summary>
        /// Locks the specified access.
        /// </summary>
        /// <param name="read">if set to <c>true</c> the acces will be lock for read depend of the locker item used.</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Lock(bool read = false)
        {
        }

        /// <summary>
        /// Locks the specified access.
        /// </summary>
        /// <param name="read">if set to <c>true</c> the acces will be lock for read depend of the locker item used.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public async Task LockAsync(bool read = false, CancellationToken token = default(CancellationToken))
        {
            await Task.Yield();
        }

        /// <summary>
        /// Releases the specified access.
        /// </summary>
        /// <param name="fromRead">if set to <c>true</c> the acces will be lock from read depend of the locker item used.</param>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public void Release(bool fromRead = false)
        {
        }

        /// <summary>
        /// Locks the specified access.
        /// </summary>
        /// <param name="read">if set to <c>true</c> the acces will be lock for read depend of the locker item used.</param>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public IDisposable ScopeLock(bool read = false)
        {
            return new Disposable();
        }

        /// <summary>
        /// Locks the specified access.
        /// </summary>
        /// <param name="read">if set to <c>true</c> the acces will be lock for read depend of the locker item used.</param>
        /// <param name="token">The token.</param>
        /// <returns></returns>
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public Task<IDisposable> ScopeLockAsync(bool read = false, CancellationToken token = default(CancellationToken))
        {
            return Task.FromResult<IDisposable>(new Disposable());
        }

        #endregion
    }
}
